package com.cuit.mimall.config.security;

import com.alibaba.fastjson.JSON;
import com.cuit.mimall.common.R;
import com.cuit.mimall.util.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author star
 * 处理认证过程中出现的异常
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) {
        R<Object> result = new R<>(HttpStatus.UNAUTHORIZED.value(), "认证失败，是不是记错了？", null);
        String json = JSON.toJSONString(result);
        WebUtils.renderString(response, json);
    }
}